﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Midgard
{
    class Artifact
    {
        List<Texture2D> textures;

        Vector2 position;
        Vector2 velocity;
        Vector2 acceleration;

        int duration;
        int frameRotationRate;
        int curFrame;
        int fadeRate;
        int fadeStartFrame;
        int curTexture;
        Color curFade;
        bool active;
        bool isLeft;
        String type;
        float curRot;

        public bool Active
        {
            get { return active; }
        }

        public Artifact(List<Texture2D> Textures, Vector2 Position, Vector2 Velolcity, Vector2 Acceleration, int Duration, int FrameRotationRate, int FadeRate, int FadeStartFrame, bool IsLeft, String Type)
        {
            textures = Textures;
            position = Position;
            velocity = Velolcity;
            acceleration = Acceleration;
            duration = Duration;
            frameRotationRate = FrameRotationRate;
            curFrame = 0;
            curTexture = 0;
            fadeRate = FadeRate;
            fadeStartFrame = FadeStartFrame;
            active = true;
            curFade = new Color(255, 255, 255);
            isLeft = IsLeft;
            type = Type;
            curRot = 0;
            position.X += textures[curTexture].Width / 2f;
            position.Y += textures[curTexture].Height / 2f;
        }

        public void Update()
        {
            position.X += velocity.X;
            position.Y += velocity.Y;

            velocity.X += acceleration.X;
            velocity.Y += acceleration.Y;

            curFrame += 1;
            if (curFrame > duration)
            {
                active = false;
            }
            curTexture = curFrame / frameRotationRate;
            if (type == "Wizard")
            {
                if (isLeft)
                {
                    curRot += -.02f;
                }
                else
                {
                    curRot += .02f;
                }
            }
            if (curTexture >= textures.Count)
            {
                curTexture = textures.Count - 1;
            }
            if (curFrame > fadeStartFrame)
            {
                byte RGB = (byte)Math.Max(255 - (fadeRate * (curFrame - fadeStartFrame)), 0);
                switch (type)
                {
                    case ("Wizard"):
                        {
                            curFade.A = RGB;
                            break;
                        }
                    case ("THOR"):
                        {
                            curFade.A = RGB;
                            break;
                        }
                    case ("ThorHurt"):
                        {
                            curFade.A = RGB;
                            break;
                        }
                    default:
                        {
                            curFade.R = RGB;
                            curFade.G = RGB;
                            curFade.B = RGB;
                            break;
                        }
                }
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(texture, new Rectangle(position.X, position.Y, Width, Height), Color.White, rotation, new Vector2(), SpriteEffects.None, 0f);

            if (!isLeft)
            {
                spriteBatch.Draw(textures[curTexture], position, null, curFade, curRot, new Vector2(textures[curTexture].Width/2f,textures[curTexture].Height/2f), 1, SpriteEffects.FlipHorizontally, 0);
            }
            else
            {
                spriteBatch.Draw(textures[curTexture], position, null, curFade, curRot, new Vector2(textures[curTexture].Width / 2f, textures[curTexture].Height / 2f), 1, SpriteEffects.None, 0);
            }
            //Console.WriteLine("Position: " + position.ToString());
        }
    }
}
